<?php

/**
 * LineasTelefonicas
 * 
 * @package Sgit 
 * @author Juan Manuel Martinez Carrillo
 * @copyright Juan Manuel Martinez Carrillo
 * @version 2012
 * @access public
 * @revision 17/02/2012 9:06:
 * 
 */



class ProveedorConsumoController extends Sgit_Controller_Action
{
    
    protected $_main_model = 'ProveedorConsumo';
    protected $_main_form = 'ProveedorConsumo';
    protected $_checkZona=TRUE;
    
    function _begin()
    {
        $this->_file_upload[1] = Sgit_Util_FileUpload::run('factura_proveedor',$this->_idZona);
    }
    
     
    
    public function indexAction()
    {


        if(!$this->_hasParam('anno')){
            $this->setParam('anno',date('Y'));
        }
        
        
        $filtros = new Sgit_Form_Filtros();
        
        $proveedores= new Sgit_Form_Element_Select_Proveedores('id_proveedor');
        $anio = new Sgit_Form_Element_Select_Year('anno');
        
        $filtros->addFormSelect($proveedores);
        $filtros->addFormSelect($anio);

        $values = $this->_getFiltrosValues($filtros);
        $filtros->addPdf();
        $filtros->addExcel();
        $filtros->myRender();
        
        $values['id_zona']=$this->_idZona;
        
        $this->view->id_proveedor=$this->_getParam('id_proveedor');


        $this->view->rows=$this->_model->default->getConsumos($values);
        $this->view->suma = Sgit_Util_Suma::run($this->view->rows, array('consumo', 'potencia', 'importe'));

        
        $this->_pdfSubTitle = $this->zona;
        $this->_export($this->view->rows);
        
        $xml = $this->_getXml($this->_actionName);
        $this->view->columnas = $xml['columnas'];


    }
    
    
    public function insertarAction()
    {
    	$this->_insertar();
    }

    public function editarAction()
    {
        $this->_editar();
    }
    
    public function borrarAction()
    {
        $this->_borrar();
    }



    
    protected function _getFila($id)
    {

        $row = $this->_model->default->getConsumo($id);
        $row = $this->_prepareValuesToTr($row);
        return $row;
    }
    
    protected function _prepareValuesToForm($row)
    {
        if($row['mes'] && $row['anno']){
            $f=new Zend_Date();
            $f->setDay(1)->setMonth($row['mes'])->setYear($row['anno'])->addMonth(1);
            
            $row['mes']=$f->get('M');
            $row['anno']=$f->get('yyyy');
        }
        return $row;
    }

    protected function _afterExecute()
    {
        $row=$this->_form->getValues();
        
        $f=new Zend_Date();
        $f->setDay(1)->setMonth($row['mes'])->setYear($row['anno'])->subMonth(1);
        
        
        $where2['id_zona']=$this->_idZona;
        $where2['mes']=$f->get('M');
        $where2['anno']=$f->get('yyyy');

        $record=$this->_model->default->getConsumosGroup($where2);
        $importe=$record[0]['importe'];
        

        $imp_data=new Application_Model_InformeElectricidadMensualDatacenter();
        $f->addMonth(1);
        $where['id_zona=?']=$this->_idZona;
        $where['mes=?']=$f->get('M');
        $where['anio=?']=$f->get('yyyy');
        
        $coste_datacenter=$imp_data->getRow($where);
        
        if(is_array($coste_datacenter)){
            $imp_data->updateRow(array('coste'=>$importe),$where);
        }
        
        
    }

    protected function _prepareValuesToDb()
    {
        
        $row=$this->_form->getValues();

        
        $f=new Zend_Date();
        $f->setDay(1)->setMonth($row['mes'])->setYear($row['anno'])->subMonth(1);
            
        $row['mes']=$f->get('M');
        $row['anno']=$f->get('yyyy');
            
       
        
        
        Sgit_Util_removeFiles::run($this->_file_upload[1]['tabla'],$this->_file_upload[1]['key'],$this->_id,$row[$this->_file_upload[1]['activo']]);
        unset($row[$this->_file_upload[1]['name']]);
        unset($row[$this->_file_upload[1]['activo']]);
            
 
        return $row;
    }
    


}
